package com.nf.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.nf.entity.ProductEntity;
import com.nf.util.ConnectionHelper;


public class DbutilsProductDao {
private static Connection con=ConnectionHelper.getConnection();
	
	public List<ProductEntity>getAll()throws SQLException{
		String sql="select * from product";
		QueryRunner qr=new QueryRunner(true);
		return qr.query(con, sql, new BeanListHandler<ProductEntity>(ProductEntity.class));
	}
	
	public int deleteByNo(String no) throws SQLException {
		String sql="delete from product where id=?";
		QueryRunner qr=new QueryRunner(true);
		return qr.update(con,sql,no);
	}
	
	public ProductEntity getInfoByNo(String no) throws SQLException {
		String sql="select * from product where id=?";
		QueryRunner qr=new QueryRunner(true);
		return qr.query(con, sql, new BeanHandler<ProductEntity>(ProductEntity.class));
	}
	
	public int update(ProductEntity product) throws SQLException {
		String sql="update product set title=?,price=?,qty=?,description=? where id=?";
		QueryRunner qr=new QueryRunner(true);
		return qr.update(con,sql,product.getTitle(),product.getPrice(),product.getQty(),product.getdescription(),product.getId());
	}
	
	public int insert(ProductEntity product) throws SQLException {
		String sql="insert into product (id,title,qty,price,description) values(?,?,?,?,?)";
		QueryRunner qr=new QueryRunner(true);
		return qr.update(con, sql, product.getId(),product.getTitle(),product.getQty(),product.getPrice(),product.getdescription());
	}
}
